package com.zzzyh.forumsystemclient.cotroller;

import com.alibaba.fastjson.JSON;
import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.model.*;
import com.zzzyh.forumsystemclient.services.IArticleService;
import com.zzzyh.forumsystemclient.services.ICommentService;
import com.zzzyh.forumsystemclient.services.IUserService;
import com.zzzyh.forumsystemclient.utils.IpUtil;
import com.zzzyh.forumsystemclient.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/protal/user/")
@Slf4j
public class ProtalUserController {
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IUserService userService;
    @RequestMapping("/myReply")
    public AppResult<PageResult<Comment>> myReply(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam(required = false) String title,
                                                  @RequestHeader("Authorization") String token){
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        List<Comment> commentList = commentService.myReply(user.getUser().getId(),pageNum, pageSize, title);
        commentList.forEach(comment -> {
            comment.setArticleTitle(articleService.getByArticleId(comment.getArticleId()).getTitle());
            comment.setNickname(userService.getUserinfoByUserId(comment.getUserId()).getNickname());
            comment.setReplyNickname(userService.getUserinfoByUserId(comment.getReplyUserId()).getNickname());
        });
        Integer replyTotal = commentService.getReplyTotal(user.getUser().getId());
        return AppResult.success(new PageResult<>(pageNum, pageSize, replyTotal, commentList));
    }

    @RequestMapping("/comment")
    public AppResult<PageResult<Comment>> userComment(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                                      @RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        log.info("用户评论：{}", user.getUser().getId());
        List<Comment> commentList = commentService.getCommentList(pageNum, pageSize, user.getUser().getId());
        log.info("用户评论：{}", commentList);
        commentList.forEach(comment -> {
            log.info("用户评论：{}", comment.getArticleId());
            Article byArticleId = articleService.getByArticleId(comment.getArticleId());
            if (byArticleId != null){
                comment.setArticleTitle(byArticleId.getTitle());
            }
        });
        Integer totalByUserId = commentService.getTotalByUserId(user.getUser().getId());
        return AppResult.success(new PageResult<>(pageNum, pageSize, totalByUserId, commentList));
    }

    @RequestMapping("/updateProfile")
    public AppResult<String> updateProfile(@RequestBody User user,
                                           HttpServletRequest request) {
        if(userService.usernameExists(user.getUsername(),user.getId()))
            return AppResult.failed("用户名已存在");
        user.setUpdateTime(new Date());
        user.setIp(IpUtil.getIp(request));
        user.setIpLocation(IpUtil.getCityInfo(IpUtil.getIp(request)));
        user.setOs(IpUtil.getOsName(request));
        user.setBrowser(IpUtil.getBrowserInfo(request));
//        String password = user.getPassword();
        user.setPassword( null);
        log.info("修改用户信息：{}", user);
        userService.updateProfile(user);
        String token = JwtUtils.generateToken(JSON.toJSONString(new LoginUser(user, null)));
        return AppResult.success("修改成功",token);
    }

    @RequestMapping("/myLike")
    public AppResult<PageResult<Article>> myLike(@RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize,
                                                @RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        List<Article> myLike = articleService.getMyLike(pageNum, pageSize, user.getUser().getId());
        Integer myLikeTotal = articleService.getMyLikeTotal(user.getUser().getId());
        return AppResult.success(new PageResult<>(pageNum, pageSize,myLikeTotal,myLike));
    }
}
